﻿using System;
using System.Collections.Generic;
using System.IO;
using Tzw.Common;

namespace Dafybjy
{
    /// <summary>
    /// 大安妇幼保健院报告接口
    /// </summary>
    public class DafybjyService
    {
        private zl9I_NewLisXML.clsI_NewLisXML _hisService;
        public bool HisInit;

        public DafybjyService()
        {
            _hisService = new zl9I_NewLisXML.clsI_NewLisXML();

            if (!_hisService.Init())
            {
                throw new Exception("InitHIS接口失败");
            }
        }

        /// <summary>
        /// 打印报告
        /// </summary>
        /// <returns></returns>
        public bool PrintReport(Report report)
        {
            var xml = "<Request><PatientID>{0}</PatientID><OrderId>{1}</OrderId></Request>";
            var outStr = "";
            //调用dll打印
            var result = _hisService.PrintRep(string.Format(xml, report.PatientID, report.OrderId), ref outStr);
            Logger.Default.Debug($"打印输出：{outStr}");
            return result;
        }

        /// <summary>
        /// 打印报告
        /// </summary>
        /// <returns></returns>
        public Response GetReportInfos(string patId)
        {
            var strIpt =
                $"<Request><PatientID>{patId}</PatientID><BarCode/><Type/><BeginTime>{DateTime.Now.AddDays(-AppSettings.ReportCfg.ReportDays):yyyy-MM-dd}</BeginTime><EndTime>{DateTime.Now.AddDays(AppSettings.ReportCfg.ReportDays):yyyy-MM-dd}</EndTime></Request>";

            Logger.Default.Debug($"传入信息:{strIpt}");
            var strOutput = "";
            _hisService.GetLisData(strIpt, ref strOutput);

            Logger.Default.Debug($"报告信息：{strOutput}");

            return XmlHelper.DeSerializer<Response>(strOutput);
        }
    }

    #region 序列化对象

    public class Response
    {
        public int ResultCode { get; set; }

        public string ErrorMsg { get; set; }

        [System.Xml.Serialization.XmlArrayItem("row")]
        public List<Report> sqldata { get; set; }
    }

    public class Report
    {
        /// <summary>
        /// 标本ID
        /// </summary>
        public string Id { get; set; }
        /// <summary>
        /// 医嘱ID
        /// </summary>
        public string OrderId { get; set; }
        /// <summary>
        /// 病人ID
        /// </summary>
        public string PatientID { get; set; }
        /// <summary>
        /// 检验项目
        /// </summary>
        public string LisItem { get; set; }
        /// <summary>
        /// 标本序号
        /// </summary>
        public string SerialNum { get; set; }
        /// <summary>
        /// 标本类型
        /// </summary>
        public string Type { get; set; }
        /// <summary>
        /// 申请时间
        /// </summary>
        public string ApplyTime { get; set; }
        /// <summary>
        /// 审核时间（审核时间为空，表示报告未出，审核时间不为空+打印次数为空或者为0，表示可打印
        /// 审核时间不为空+打印次数大于0，表示不可打印。（自助报告机只能打印一次）
        /// </summary>
        public string CheckTime { get; set; }
        /// <summary>
        /// 审核人
        /// </summary>
        public string Checker { get; set; }
        /// <summary>
        /// 样本条码
        /// </summary>
        public string SampleCode { get; set; }
        /// <summary>
        /// 病人来源
        /// </summary>
        public string Source { get; set; }
        /// <summary>
        /// 打印次数
        /// </summary>
        public int PrintNum { get; set; }

    }

    #endregion
}